package org.jeecg.modules.finance.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.entity.BusRoomPayType;
import org.jeecg.modules.business.service.IBusRoomPayTypeService;
import org.jeecg.modules.finance.mapper.SummaryMapper;
import org.jeecg.modules.finance.service.ISummaryService;
import org.jeecg.modules.finance.vo.PayTypeStatVo;
import org.jeecg.modules.finance.vo.RoomStatVo;
import org.jeecg.modules.fw.entity.FwRoomRepair;
import org.jeecg.modules.fw.mapper.FwRoomRepairMapper;
import org.jeecg.modules.fw.service.IFwRoomRepairService;
import org.jeecg.modules.pos.entity.PosOrderGoodsPayment;
import org.jeecg.modules.rooms.mapper.CesRoomLayoutPriceDateMapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.*;

/**
 * @Description: 收款汇总统计
 * @Author: jeecg-boot
 * @Date:   2023-04-19
 * @Version: V1.0
 */
@Service
public class SummaryServiceImpl extends ServiceImpl<SummaryMapper, PosOrderGoodsPayment> implements ISummaryService {

    @Resource
    private SummaryMapper summaryMapper;
    @Resource
    private IBusRoomPayTypeService busRoomPayTypeService;

    /**
     * 收款汇总统计
     *
     * @param page
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public Page<HashMap<String, Object>> pageList(Page<HashMap<String, Object>> page, String hotelId, DateTime startTime, DateTime endTime, Integer departmentId) {
        LambdaQueryWrapper<BusRoomPayType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusRoomPayType::getHotelId, hotelId);
        List<BusRoomPayType> busRoomPayTypes = busRoomPayTypeService.list(lambdaQueryWrapper);
        if (departmentId == 1) {
            return page.setRecords(summaryMapper.posPageList(page, busRoomPayTypes, hotelId, startTime, endTime));
        } else if (departmentId == 2) {
            return page.setRecords(summaryMapper.roomPageList(page, busRoomPayTypes, hotelId, startTime, endTime));
        }
        return page.setRecords(summaryMapper.pageList(page, busRoomPayTypes, hotelId, startTime, endTime));
    }

    /**
     * 收退款明细列表
     *
     * @param page
     * @param hotelId
     * @param startTime
     * @param endTime
     * @param departmentId
     * @return
     */
    public Page<HashMap<String, Object>> financePageList(Page<HashMap<String, Object>> page, String hotelId, DateTime startTime, DateTime endTime, Integer departmentId) {
        LambdaQueryWrapper<BusRoomPayType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusRoomPayType::getHotelId, hotelId);
        if (departmentId == 1) {
            return page.setRecords(summaryMapper.postFinancePage(page, hotelId, startTime, endTime));
        } else if (departmentId == 2) {
            return page.setRecords(summaryMapper.roomFinancePage(page, hotelId, startTime, endTime));
        }
        return page.setRecords(summaryMapper.financePage(page, hotelId, startTime, endTime));
    }


    /**
     * 按时间每天收入统计列表
     *
     * @param hotelId   酒店id
     * @param startTime 开始日期
     * @param endTime   截至日期
     * @param section   1天 2月 3季度 4年
     * @return
     */
    public List<HashMap<String, Object>> dayShouRuStatList(String hotelId, DateTime startTime, DateTime endTime, Integer section) {
        if (section.equals(1)) {
            return summaryMapper.dayShouRuStatList(hotelId, startTime, endTime);
        } else if (section.equals(2)) {
            ArrayList<Integer> months = new ArrayList<Integer>();
            Temporal temporal1 = LocalDate.parse(startTime.toDateStr());
            Temporal temporal2 = LocalDate.parse(endTime.toDateStr());
            long sum = ChronoUnit.MONTHS.between(temporal1, temporal2);
            for (Integer i = 1; i <= sum; i++) {
                months.add(i);
            }
            return summaryMapper.monthShouRuStatList(hotelId, startTime, endTime, months);
        } else if (section.equals(3)) {
            ArrayList<Integer> quarters = new ArrayList<Integer>();
            Temporal temporal1 = LocalDate.parse(startTime.toDateStr());
            Temporal temporal2 = LocalDate.parse(endTime.toDateStr());
            long sum = ChronoUnit.MONTHS.between(temporal1, temporal2) / 3;
            for (Integer i = 1; i <= sum; i++) {
                quarters.add(i);
            }
            return summaryMapper.quarterShouRuStatList(hotelId, startTime, endTime, quarters);
        } else if (section.equals(4)) {
            ArrayList<Integer> years = new ArrayList<Integer>();
            Temporal temporal1 = LocalDate.parse(startTime.toDateStr());
            Temporal temporal2 = LocalDate.parse(endTime.toDateStr());
            long sum = ChronoUnit.MONTHS.between(temporal1, temporal2) / 12;
            for (Integer i = 1; i <= sum; i++) {
                years.add(i);
            }
//            throw  new JeecgBootException(""+sum);
            return summaryMapper.yearShouRuStatList(hotelId, startTime, endTime, years);
        }
        return null;
    }

    /**
     * 按收款方式收入统计列表
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HashMap<String, Object>> payMethodStatList(String hotelId, DateTime startTime, DateTime endTime){
        return summaryMapper.payMethodStatList(hotelId, startTime, endTime);
    }

    /**
     * 按房型收入统计列表
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HashMap<String, Object>> layoutStatList(String hotelId, DateTime startTime, DateTime endTime){
        return summaryMapper.layoutStatList(hotelId, startTime, endTime);
    }

    /**
     * 按费项收入统计列表
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HashMap<String, Object>> subjectTypeStatList(String hotelId, DateTime startTime, DateTime endTime){
        LambdaQueryWrapper<BusRoomPayType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusRoomPayType::getHotelId, hotelId);
        List<BusRoomPayType> busRoomPayTypes = busRoomPayTypeService.list(lambdaQueryWrapper);
        return summaryMapper.subjectTypeStatList(hotelId, busRoomPayTypes,startTime, endTime);
    }

    /**
     * 按来源各项统计列表
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HashMap<String, Object>> livingSourceStatList( String hotelId, DateTime startTime,DateTime endTime)
    {
        return summaryMapper.livingSourceStatList(hotelId,startTime, endTime);
    }

    /**
     * 按客型各项统计列表
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HashMap<String, Object>> customerTypeStatList(String hotelId,  DateTime startTime,  DateTime endTime) {
        return summaryMapper.customerTypeStatList(hotelId, startTime, endTime);
    }

    /**
     * 按入住类型各项统计列表
     * @param hotelId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HashMap<String, Object>> livingTypeStatList(String hotelId,  DateTime startTime,  DateTime endTime) {
        return summaryMapper.livingTypeStatList(hotelId, startTime, endTime);
    }

    /**
     * 按房间统计当日收入明细列表
     * @param hotelId
     * @return
     */
    public List<RoomStatVo> roomStatList(String hotelId,  DateTime startTime,  DateTime endTime) {
        List<RoomStatVo> list = summaryMapper.roomStatList(hotelId, startTime, endTime);
        if (ObjectUtils.isNotEmpty(list)) {
            for (RoomStatVo roomStatVo : list) {
                List<HashMap<String, Object>> detailList = summaryMapper.roomStatDeatilList(roomStatVo.getRoomId(), startTime, endTime);
                roomStatVo.setDetailList(detailList);
            }
        }
        return list;
    }

    /**
     * 按时间统计当日收入明细列表
     * @param hotelId
     * @return
     */
    public List<HashMap<String, Object>> dayOrderFeeList(String hotelId,  DateTime startTime,  DateTime endTime){
        return summaryMapper.dayOrderFeeList(hotelId, startTime, endTime);
    }

    /**
     * 按收款方式统计当日收入明细列表
     * @param hotelId
     * @return
     */
    public List<PayTypeStatVo> dayPayTypeList(String hotelId) {
        List<PayTypeStatVo> list = new ArrayList<>();
        LambdaQueryWrapper<BusRoomPayType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusRoomPayType::getHotelId, hotelId);
        List<BusRoomPayType> busRoomPayTypes = busRoomPayTypeService.list(lambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(busRoomPayTypes)) {
            for (BusRoomPayType busRoomPayType : busRoomPayTypes) {
                PayTypeStatVo vo = new PayTypeStatVo();
                List<HashMap<String, Object>> detailList = summaryMapper.dayPayTypeDetailList(busRoomPayType.getId());
                vo.setPayTypeName(busRoomPayType.getName());
                vo.setDetailList(detailList);
                list.add(vo);
            }
        }
        return list;
    }

    /**
     * 按事项统计当日支出明细列表
     * @param hotelId
     * @return
     */
    public List<HashMap<String, Object>> dayMatterExpendDetailList(String hotelId) {
        return summaryMapper.dayMatterExpendDetailList(hotelId);
    }

    /**
     * 按时间统计当日支出明细列表
     * @param hotelId
     * @return
     */
    public List<HashMap<String, Object>> dayExpendOrderFeeList(String hotelId) {
        return summaryMapper.dayExpendOrderFeeList(hotelId);
    }

    /**
     * 按退款方式统计当日支出明细列表
     * @param hotelId
     * @return
     */
    public List<PayTypeStatVo> dayPayTypeExpendList(String hotelId) {
        List<PayTypeStatVo> list = new ArrayList<>();
        LambdaQueryWrapper<BusRoomPayType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusRoomPayType::getHotelId, hotelId);
        List<BusRoomPayType> busRoomPayTypes = busRoomPayTypeService.list(lambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(busRoomPayTypes)) {
            for (BusRoomPayType busRoomPayType : busRoomPayTypes) {
                PayTypeStatVo vo = new PayTypeStatVo();
                List<HashMap<String, Object>> detailList = summaryMapper.dayPayTypeExpendDetailList(busRoomPayType.getId());
                vo.setPayTypeName(busRoomPayType.getName());
                vo.setDetailList(detailList);
                list.add(vo);
            }
        }
        return list;
    }

    /**
     * 收支情况统计
     * @param hotelId
     * @param section 1近7天 2本月 3近半年 4本年度
     * @return
     */
    public List<HashMap<String, Object>> financeSummary(String hotelId,Integer section) {
        if (section.equals(1)) {
            return summaryMapper._7DayFinanceSummary(hotelId, 7);
        } else if (section.equals(2)) {
            return summaryMapper.monthFinanceSummary(hotelId);
        } else if (section.equals(3)) {
            return summaryMapper._7DayFinanceSummary(hotelId, 182);
        } else if (section.equals(4)) {
            return summaryMapper.yearFinanceSummary(hotelId);
        }
        return null;
    }
}
